package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.PersonnelArchive;
import com.amsp.oa.oademo.mapper.PersonnelArchiveMapper;
import com.amsp.oa.oademo.service.PersonnelArchiveService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class PersonnelArchiveServiceImpl extends ServiceImpl<PersonnelArchiveMapper, PersonnelArchive>
        implements PersonnelArchiveService {

    @Override
    public IPage<PersonnelArchive> getPage(Integer pageNum, Integer pageSize, String employeeName,
            String employeeNumber) {
        LambdaQueryWrapper<PersonnelArchive> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.hasText(employeeName)) {
            wrapper.like(PersonnelArchive::getEmployeeName, employeeName);
        }

        if (StringUtils.hasText(employeeNumber)) {
            wrapper.like(PersonnelArchive::getEmployeeNumber, employeeNumber);
        }

        wrapper.orderByDesc(PersonnelArchive::getCreateTime);

        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateBatchTransactional(List<PersonnelArchive> archives) {
        saveOrUpdateBatch(archives, 100);
    }
}